package com.struts.deal.admin.corpor;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import com.base.Constants;
import com.ibatis.po.corpor.TActivity;
import com.ibatis.po.corpor.TApplyBooth;
import com.ibatis.po.corpor.TBooth;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.lg.system.CDBManager;
import com.lg.system.CSqlMap;
import com.lg.system.CTurnPage;
import com.lg.system.Common;
import com.lg.system.MessageBean;
import com.struts.web.manage.admin.corpor.ApproveCorporApplyForm;
import com.struts.web.manage.admin.corpor.CreateActivityForm;
import com.struts.web.manage.admin.corpor.ViewBoothForm;

public class BoothDeal {

	/**
	 * 展位浏览
	 * @param request
	 */
	public static  void viewActivityList(HttpServletRequest request){

		CTurnPage page = new CTurnPage();
		try{
			page.setQueryList("T_ACTIVITY.selectShowDateList");
			page.setPerPageRows(10);//设置每页行数
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 添加现场招聘
	 * @param request
	 */
	public static  boolean createActivity(HttpServletRequest request,CreateActivityForm aWebForm ) throws Exception{
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TActivity activityPo = new TActivity();
		boolean result = false;
		TBooth boothPo = new TBooth();
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		dateFormat.setLenient(false);
		java.util.Date timeDate = dateFormat.parse(aWebForm.getShowDate());
		java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());
		try{
			activityPo.setAddress(aWebForm.getAddress());
			activityPo.setCreatePerson(aWebForm.getCreatePerson());
			activityPo.setCreateTime(new java.util.Date());
			activityPo.setId(Common.getNewID());
			activityPo.setShowDate(Date.valueOf(aWebForm.getShowDate()));
			activityPo.setStatus(aWebForm.getStatus());
			
			sqlmap.insert("T_ACTIVITY.abatorgenerated_insert",activityPo);

			for(int i=1;i<=106;i++){
				boothPo.setShowDate(dateTime);
				boothPo.setBoothNo(String.valueOf(i));
				boothPo.setOrderNo(String.valueOf(i));
				boothPo.setStatus("0");
				for(int j=0;j<3;j++){
					boothPo.setId(Common.getNewID());
					boothPo.setTimeType(String.valueOf(j));
					sqlmap.insert("T_BOOTH.abatorgenerated_insert",boothPo);
				}
			}
			result = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 修改现场招聘
	 * @param request
	 */
	public static  MessageBean editActivity(HttpServletRequest request,CreateActivityForm aWebForm ) throws Exception{
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TActivity activityPo = new TActivity();
		MessageBean messageBean = new MessageBean();
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		dateFormat.setLenient(false);
		int count=0;
		try{
			activityPo.setId(aWebForm.getActivityId());
			activityPo.setAddress(aWebForm.getAddress());
			activityPo.setCreatePerson(aWebForm.getCreatePerson());
			activityPo.setCreateTime(new java.util.Date());
			if(aWebForm.getShowDate()!=null && aWebForm.getShowDate()!=""){
				java.util.Date timeDate = dateFormat.parse(aWebForm.getShowDate());
				java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());
				activityPo.setShowDate(dateTime);
                  //检查活动是否可修改
				count = (Integer)sqlmap.queryForObject("T_ACTIVITY.selectForActivityIsCanEditById",activityPo);

			}

			activityPo.setStatus(aWebForm.getStatus());
			if(count<1){
				sqlmap.update("T_BOOTH.updateBoothDate",activityPo);
				count = sqlmap.update("T_ACTIVITY.updateById",activityPo);
				if(count>0){
					messageBean.setResult(true);
				}else{
					messageBean.setMessage(Constants.EDIT_FAIL);
				}
			}else{
				System.out.println("3333333333333333:");
				messageBean.setMessage(Constants.CORPOR_USED);
			}

		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 * 根据id加载现场招聘信息
	 * @param request
	 */
	public static  MessageBean loalActivity(HttpServletRequest request,CreateActivityForm aWebForm ) throws Exception{
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TActivity activityPo = new TActivity();
		String aId = request.getParameter("aId");
		MessageBean messageBean = new MessageBean();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try{
			activityPo.setId(aId);
			activityPo = (TActivity)sqlmap.queryForObject("T_ACTIVITY.selectById",activityPo);
			aWebForm.setCreatePerson(activityPo.getCreatePerson());
			aWebForm.setAddress(activityPo.getAddress());
			aWebForm.setShowDate(df.format(activityPo.getShowDate()));
			aWebForm.setStatus(activityPo.getStatus());
			aWebForm.setActivityId(aId);
			messageBean.setResult(true);
		}catch(Exception e){
			messageBean.setMessage(Constants.LOAD_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	/**
	 * 删除现场招聘
	 * @param request
	 */
	public static  MessageBean delActivity(HttpServletRequest request,CreateActivityForm aWebForm ) throws Exception{
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TActivity activityPo = new TActivity();
		activityPo.setId(aWebForm.getActivityId());
		MessageBean messageBean = new MessageBean();
		int count = 0;
		try{
//			检查活动是否可修改
			count = (Integer)sqlmap.queryForObject("T_ACTIVITY.selectForActivityIsCanEditById",activityPo);
			if(count<1){
				sqlmap.delete("T_BOOTH.deleteBoothByAcId",activityPo);
				count= sqlmap.delete("T_ACTIVITY.deleteById",activityPo);

				if(count>0){
					messageBean.setResult(true);

				}else{
					messageBean.setMessage(Constants.DEL_FAIL);
				}
			}else{
//				检查是否可删除
				count = (Integer)sqlmap.queryForObject("T_ACTIVITY.selectForActivityIsCanDel",activityPo);
				if(count>0){
					sqlmap.delete("T_BOOTH.deleteBoothByAcId",activityPo);
					count= sqlmap.delete("T_ACTIVITY.deleteById",activityPo);

					if(count>0){
						messageBean.setResult(true);

					}else{
						messageBean.setMessage(Constants.DEL_FAIL);
					}
				}else{
					messageBean.setMessage(Constants.CORPOR_USED);
				}

			}


		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 * 展位浏览
	 * @param request
	 */
	public static  void viewBooth(HttpServletRequest request,ViewBoothForm aWebForm ) throws Exception{

		CTurnPage page = new CTurnPage();
		TBooth boothPo = new TBooth();
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		dateFormat.setLenient(false);

		try{
			if(aWebForm.getBoothNo()!=null && aWebForm.getBoothNo()!=""){
				boothPo.setBoothNo(aWebForm.getBoothNo());
			}
			if(aWebForm.getShowDate()!=null && aWebForm.getShowDate()!=""){
				java.util.Date timeDate = dateFormat.parse(aWebForm.getShowDate());
				java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());
				boothPo.setShowDate(dateTime);
			}
			if(aWebForm.getCorporName()!=null && aWebForm.getCorporName()!=""){
				boothPo.setCorporName(aWebForm.getCorporName());
			}
			if(aWebForm.getCorporId()!=null && aWebForm.getCorporId()!=""){
				boothPo.setCorporId(aWebForm.getCorporId());
			}

			page.setQueryList("T_BOOTH.selectAllBooth",boothPo);
			page.setPerPageRows(10);
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}


	
	/**
	 * 导出现场招聘Excel
	 * @param request
	 */
	public static  void exportBoothCorpor(HttpServletRequest request,ViewBoothForm aWebForm){

		CTurnPage page = new CTurnPage();
		TApplyBooth applyPo = new TApplyBooth();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		try{
			if(aWebForm.getAppDate()!=null && aWebForm.getAppDate()!=""){
				java.util.Date dappDate = df.parse(aWebForm.getAppDate());
				Timestamp appDate = new java.sql.Timestamp(dappDate.getTime());
				applyPo.setAppDate(appDate);
			}
			applyPo.setAppStatus(aWebForm.getStatus());
			page.setQueryList("T_APPLY_BOOTH.selectSituationApplyByCorpor",applyPo);
			page.setPerPageRows(1000);
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 申请展位浏览
	 * @param request
	 */
	public static  void approveBooth(HttpServletRequest request,ViewBoothForm aWebForm){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		CTurnPage page = new CTurnPage();
		List situationList = null;
		TApplyBooth applyPo = new TApplyBooth();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try{
			if(aWebForm.getAppDate()!=null && aWebForm.getAppDate()!=""){
				java.util.Date dappDate = df.parse(aWebForm.getAppDate());
				Timestamp appDate = new java.sql.Timestamp(dappDate.getTime());
				applyPo.setAppDate(appDate);
			}


			applyPo.setCorporId(aWebForm.getCorporId());
			applyPo.setCorporName(aWebForm.getCorporName());

			applyPo.setAppStatus(aWebForm.getStatus());

			situationList = sqlmap.queryForList("T_BOOTH.selectSituationOfBooth");
			page.setQueryList("T_APPLY_BOOTH.selectSituationApplyByCorpor",applyPo);
			page.setPerPageRows(10);
			page.excute(request);
			request.setAttribute("situationList", situationList);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	
	/**
	 * 导出发布职位
	 * @param request
	 */
	public static  void exportPost(HttpServletRequest request,ViewBoothForm aWebForm){
		CTurnPage page = new CTurnPage();
		TApplyBooth applyPo = new TApplyBooth();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try{
			if(aWebForm.getAppDate()!=null && aWebForm.getAppDate()!=""){
				java.util.Date dappDate = df.parse(aWebForm.getAppDate());
				Timestamp appDate = new java.sql.Timestamp(dappDate.getTime());
				applyPo.setAppDate(appDate);
			}

            System.out.println("date:"+applyPo.getAppDate()+":"+aWebForm.getAppDate());

			applyPo.setAppStatus(aWebForm.getStatus());

			page.setQueryList("T_APPLY_BOOTH.exportPost",applyPo);
			page.setPerPageRows(10000);
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 申请展位批准信息加载
	 * @param request
	 */
	public static  void loadApproveCorporApply(HttpServletRequest request,ApproveCorporApplyForm aWebForm) throws SQLException{
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		String applyId = request.getParameter("applyId");
		TApplyBooth applyBoothPo = new TApplyBooth();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try{
			if(applyId!=null && applyId.length()>0){
				applyBoothPo = (TApplyBooth)sqlmap.queryForObject("T_APPLY_BOOTH.selectCorporApply",applyId);
				if(applyBoothPo!=null){
					aWebForm.setAppDate(df.format(applyBoothPo.getAppDate()));
					aWebForm.setCorporName(applyBoothPo.getCorporName());
					aWebForm.setAppTime(applyBoothPo.getAppTime());
					aWebForm.setRecruitContent(applyBoothPo.getRecruitContent());
					request.setAttribute("applyId", applyId);
				}
			}

		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 申请展位批准
	 * @param request
	 */
	public static  MessageBean approveCorporApply(HttpServletRequest request,ApproveCorporApplyForm aWebForm) throws Exception{

		Connection conn=null;
		PreparedStatement psmt=null;
		ResultSet rs = null;
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		String applyId = request.getParameter("applyId");
		TApplyBooth applyBoothPo = new TApplyBooth();
		TBooth  boothPo = new TBooth();
		MessageBean messageBean = new MessageBean();
		DateFormat dateFormat;
		dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		dateFormat.setLenient(false);
		java.util.Date timeDate = dateFormat.parse(aWebForm.getAppDate());
		java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());//sql类型


		String sql = "select count(*) from t_booth t where t.status = '0' and t.timetype = ? and trunc(t.showdate,'dd')=trunc(?,'dd')";

		try{
			conn = CDBManager.getConn();
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, aWebForm.getAppTime());
			psmt.setTimestamp(2, dateTime);

			boothPo.setApplyId(applyId);
			boothPo.setShowDate(dateTime);
			boothPo.setTimeType(aWebForm.getAppTime());

			applyBoothPo.setId(applyId);
			applyBoothPo.setAppStatus("1");
			applyBoothPo.setAppSuccessTime(aWebForm.getAppTime());

			rs = psmt.executeQuery();

			if(rs.next()){
				if(rs.getInt(1)>0){
					sqlmap.update("T_BOOTH.autoUpdateForBooth",boothPo);
					sqlmap.update("T_APPLY_BOOTH.updateAppStatus", applyBoothPo);
					messageBean.setResult(true);
				}
			}else{
				messageBean.setResult(false);
				messageBean.setMessage(Constants.BOOTH_FULL);
			}
		}catch(Exception e){
			messageBean.setMessage(Constants.ADD_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 * 拒绝申请
	 * @param request
	 */
	public static  boolean refuseCorporApply(HttpServletRequest request,ApproveCorporApplyForm aWebForm) throws SQLException{
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		String applyId = request.getParameter("applyId");
		String id = applyId;
		TApplyBooth applyBoothPo = new TApplyBooth();
		TApplyBooth applyBoothPo1 = new TApplyBooth();
		TBooth boothPo = new TBooth();
		boolean result = false;
		DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		try{

			applyBoothPo1=(TApplyBooth)sqlmap.queryForObject("T_APPLY_BOOTH.selectCorporApply",id);
			java.util.Date dshowDate = ft.parse(ft.format(applyBoothPo1.getAppDate()));
			Timestamp showDate = new java.sql.Timestamp(dshowDate.getTime());

			boothPo.setApplyId(applyId);
			boothPo.setShowDate(showDate);


			applyBoothPo.setId(applyId);
			applyBoothPo.setAppStatus(aWebForm.getRefuseType());
			sqlmap.update("T_APPLY_BOOTH.updateAppStatus", applyBoothPo);
			sqlmap.update("T_BOOTH.updateBoothByCorRefuse",boothPo);
			result = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
}
